package se.kth.seds.mi.ui;

import se.kth.seds.mi.core.common.HashAlgorithm;
import se.kth.seds.mi.core.crypto.sharedsecret.SharedSecretCrypto;
import se.kth.seds.mi.core.exceptions.OperationFailedException;
import se.kth.seds.mi.communication.Server;

import javax.swing.*;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Sike Huang
 * Date: Nov 20, 2008
 * Time: 12:35:40 AM
 * To change this template use File | Settings | File Templates.
 */
public class SharedSecretReceiverFrame extends javax.swing.JFrame {

    private String sharedSecret;
    private Server server;
    private SharedSecretCrypto sharedSecretCrypto;

    private String receivedMessage;
    private String receivedMac;

    public void setSharedSecret(String sharedSecret) {
        this.sharedSecret = sharedSecret;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    public void setSharedSecretCrypto(SharedSecretCrypto sharedSecretCrypto) {
        this.sharedSecretCrypto = sharedSecretCrypto;
    }

    public void setReceivedMac(String receivedMac) {
        jTextArea_receivedMac.setText(receivedMac);
        this.receivedMac = receivedMac;
    }

    public void setReceivedMessage(String receivedMessage) {
        jTextArea_receivedMessage.setText(receivedMessage);
        this.receivedMessage = receivedMessage;
    } /** Creates new form SharedSecretReceiverFrame */
//    public SharedSecretReceiverFrame() {
//        initComponents();
//    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public void initComponents() {

        jLabel_heading = new javax.swing.JLabel();
        jLabel_receivedMessage = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_receivedMessage = new javax.swing.JTextArea();
        jTextField_sharedSecret = new javax.swing.JTextField();
        jLabel_sharedSecret = new javax.swing.JLabel();
        jLabel_hashFunc = new javax.swing.JLabel();
        jComboBox_hashFunc = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea_receivedMac = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea_generatedMac = new javax.swing.JTextArea();
        jLabel_receivedMac = new javax.swing.JLabel();
        jLabel_generatedMac = new javax.swing.JLabel();
        jButton_verify = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jLabel_heading.setText("Shared Secret Receiver"); // NOI18N
        jLabel_heading.setName("jLabel_heading"); // NOI18N

        jLabel_receivedMessage.setText("Received Message:"); // NOI18N
        jLabel_receivedMessage.setName("jLabel_receivedMessage"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea_receivedMessage.setColumns(20);
        jTextArea_receivedMessage.setEditable(false);
        jTextArea_receivedMessage.setRows(5);
        jTextArea_receivedMessage.setName("jTextArea_receivedMessage"); // NOI18N
        jTextArea_receivedMessage.setLineWrap(true);
        jScrollPane1.setViewportView(jTextArea_receivedMessage);

        jTextField_sharedSecret.setText(""); // NOI18N
        jTextField_sharedSecret.setName("jTextField_sharedSecret"); // NOI18N

        jLabel_sharedSecret.setText("Shared Secret:"); // NOI18N
        jLabel_sharedSecret.setName("jLabel_sharedSecret"); // NOI18N

        jLabel_hashFunc.setText("Hash Func:"); // NOI18N
        jLabel_hashFunc.setName("jLabel_hashFunc"); // NOI18N

        jComboBox_hashFunc.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
        jComboBox_hashFunc.setName("jComboBox_hashFunc"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea_receivedMac.setColumns(20);
        jTextArea_receivedMac.setEditable(false);
        jTextArea_receivedMac.setRows(5);
        jTextArea_receivedMac.setName("jTextArea_receivedMac"); // NOI18N
        jTextArea_receivedMac.setLineWrap(true);
        jScrollPane2.setViewportView(jTextArea_receivedMac);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTextArea_generatedMac.setColumns(20);
        jTextArea_generatedMac.setEditable(false);
        jTextArea_generatedMac.setRows(5);
        jTextArea_generatedMac.setName("jTextArea_generatedMac"); // NOI18N
        jTextArea_generatedMac.setLineWrap(true);
        jScrollPane3.setViewportView(jTextArea_generatedMac);

        jLabel_receivedMac.setText("Received MAC:"); // NOI18N
        jLabel_receivedMac.setName("jLabel_receivedMac"); // NOI18N

        jLabel_generatedMac.setText("Generated MAC:"); // NOI18N
        jLabel_generatedMac.setName("jLabel_generatedMac"); // NOI18N

        jButton_verify.setText("Verify"); // NOI18N
        jButton_verify.setName("jButton_verify"); // NOI18N
        jButton_verify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_verifyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(122, Short.MAX_VALUE)
                                .addComponent(jLabel_heading)
                                .addGap(119, 119, 119))
                        .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_receivedMessage)
                                .addComponent(jLabel_sharedSecret)
                                .addComponent(jLabel_hashFunc)
                                .addComponent(jLabel_receivedMac)
                                .addComponent(jLabel_generatedMac))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton_verify)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jComboBox_hashFunc, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTextField_sharedSecret, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)))
                        .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel_heading)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_receivedMessage)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField_sharedSecret, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_sharedSecret))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel_hashFunc)
                                .addComponent(jComboBox_hashFunc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_receivedMac))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_generatedMac)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jButton_verify)
                        .addContainerGap(17, Short.MAX_VALUE))
        );

        jComboBox_hashFunc.setModel(new DefaultComboBoxModel(HashAlgorithm.values()));
        jTextField_sharedSecret.setText(sharedSecret);

        pack();
        setVisible(true);

        try {
            server.read(this);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }// </editor-fold>

    private void jButton_verifyActionPerformed(java.awt.event.ActionEvent evt) {
        String sharedSecret = jTextField_sharedSecret.getText();
        if (receivedMac == null || receivedMac.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "No message has been received", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }
        // calculate MAC on server side, and compare it with the one received from client
        HashAlgorithm hashAlgorithm = (HashAlgorithm) jComboBox_hashFunc.getSelectedItem();
        sharedSecretCrypto.setHashAlgorithm(hashAlgorithm);
        sharedSecretCrypto.setMessage(receivedMessage);
        sharedSecretCrypto.setSharedSecret(sharedSecret);
        try {
            String mac = sharedSecretCrypto.createMAC();
            jTextArea_generatedMac.setText(mac);
            if (mac.equals(receivedMac)) {
                JOptionPane.showMessageDialog(this, "Message integrity is valid", "Verification", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "Message integrity is invalid! (hint: please check your shared secert and hash func)", "Verification", JOptionPane.ERROR_MESSAGE);
            }
        } catch (OperationFailedException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            JOptionPane.showMessageDialog(this, e1.getMessage(), "Crypto Exception", JOptionPane.ERROR_MESSAGE);
        }
    }


    // Variables declaration - do not modify
    private javax.swing.JButton jButton_verify;
    private javax.swing.JComboBox jComboBox_hashFunc;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel_generatedMac;
    private javax.swing.JLabel jLabel_hashFunc;
    private javax.swing.JLabel jLabel_heading;
    private javax.swing.JLabel jLabel_receivedMac;
    private javax.swing.JLabel jLabel_receivedMessage;
    private javax.swing.JLabel jLabel_sharedSecret;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea_generatedMac;
    private javax.swing.JTextArea jTextArea_receivedMac;
    private javax.swing.JTextArea jTextArea_receivedMessage;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField_sharedSecret;
    // End of variables declaration

}
